﻿using System;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using Lswweb.Domain;
using Lswweb.Model;

namespace Lswweb.Controls
{
    public class NewsShow : SkinnedWebControl
    {
        private ITextControl lbTitle;
        private ITextControl lbSummary;
        private ITextControl lbCreatedDate;
        private ITextControl lbSource;
        private ITextControl lbBody;
        private Image img;

        protected override void InitializeSkin(Control skin)
        {
            lbTitle = (ITextControl)skin.FindControl("lbTitle");
            lbSummary = (ITextControl)skin.FindControl("lbSummary");
            lbCreatedDate = (ITextControl)skin.FindControl("lbCreatedDate");
            lbSource = (ITextControl)skin.FindControl("lbSource");
            lbBody = (ITextControl)skin.FindControl("lbBody");
            img = (Image)skin.FindControl("img");

            NewsInfo newsInfo = Info;
            if (newsInfo != null)
            {
                Page.Title = newsInfo.Title;
                Page.Description = newsInfo.Summary;
                Page.Keywords = newsInfo.Keys;

                if(lbTitle!=null)
                    lbTitle.Text = newsInfo.Title;
                
                if(lbCreatedDate!=null)
                    lbCreatedDate.Text = Globals.FormatDateTime(newsInfo.DateCreated, DateTimeFormatMode.DateTime);
                
                if(lbSource!=null)
                    lbSource.Text = newsInfo.Source;
                
                if(lbBody!=null)
                    lbBody.Text = newsInfo.Content;

                if (lbSummary != null)
                    lbSummary.Text = newsInfo.Summary;

                if (img != null)
                {
                    if (!string.IsNullOrEmpty(newsInfo.ImageUrl))
                        img.ImageUrl = newsInfo.ImageUrl;
                    else
                        img.Visible = false;
                }
            }
        }

        private NewsInfo info;
        public NewsInfo Info
        {
            get 
            { 
                if(info==null)
                    info = News.GetNews(SiteContext.Current.Id);

                if (info == null)
                    throw new SiteException(ExceptionType.ResourceNotFound);

                return info;
            }
            set { info = value; }
        }

        protected override string DefaultSkinFilename
        {
            get { return "News/NewsShow.ascx"; }
        }
    }
}
